package com.wf.redis;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author wufei
 * @date 2018/3/20/020 11:56
 */
public class RedisManagerCluster {

    public static void main(String[] args) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // 最大连接数
        poolConfig.setMaxTotal(1);
        // 最大空闲数
        poolConfig.setMaxIdle(1);
        // 最大允许等待时间，如果超过这个时间还未获取到连接，则会报JedisException异常：
        // Could not get a resource from the pool
        poolConfig.setMaxWaitMillis(1000);
        Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
        nodes.add(new HostAndPort("10.211.55.6", 7000));
        nodes.add(new HostAndPort("10.211.55.6", 7001));
        nodes.add(new HostAndPort("10.211.55.6", 7002));
        JedisCluster cluster = new JedisCluster(nodes, poolConfig);
        String name = cluster.get("name");
        System.out.println(name);
        cluster.set("age", "18");
        System.out.println(cluster.get("age"));

        cluster.sadd("sadd","a","b","c");
        System.out.println(cluster.smembers("sadd").size());
        try {
            cluster.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
